package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.dto.NursingPlanListDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Slf4j
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);

        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlanListDto 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlanVo> selectNursingPlanList(NursingPlanListDto nursingPlanListDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanListDto, NursingPlan.class);
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //删除缓存
        redisCache.deleteObject(CacheConstants.NURSING_PLAN_CACHE_KEY);
        //插入护理计划表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        log.info("将前端的请求dto对象内的数据，转化成entity对象内的数据{}", nursingPlan);
        this.save(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(n -> {
            n.setPlanId(nursingPlan.getId());
        });

        return nursingProjectPlanService.saveBatch(projectPlans) ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //删除缓存
        redisCache.deleteObject(CacheConstants.NURSING_PLAN_CACHE_KEY);

        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            // 删除护理计划关联的护理项目
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingProjectPlan::getPlanId, nursingPlan.getId());
            nursingProjectPlanMapper.delete(queryWrapper);

            //  批量新增护理计划关联的护理项目
            projectPlans.forEach(n -> {
                n.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }

        //修改护理计划表数据
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        //删除缓存
        redisCache.deleteObject(CacheConstants.NURSING_PLAN_CACHE_KEY);
        // 删除护理计划关联的护理项目
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId, id);
        nursingProjectPlanMapper.delete(queryWrapper);

        // 删除护理计划表数据
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有的护理计划
     * */
    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> cacheList = redisCache.getCacheList(CacheConstants.NURSING_PLAN_CACHE_KEY);
        if(CollUtil.isEmpty(cacheList)){
            LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingPlan::getStatus, 1);
            cacheList = nursingPlanMapper.selectList(queryWrapper);
            redisCache.setCacheList(CacheConstants.NURSING_PLAN_CACHE_KEY, cacheList);
        }
        return cacheList;
    }
}
